package study.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import study.entity.RestResponse;
import study.service.RestWrapper;
import study.util.RestUtil;

/**
 * @author sunxuejian
 * @version 1.0
 * @date 2020/3/12 19:05
 */
@RestController
public class UaosController1 {

	@Autowired
	RestWrapper restWrapper;

	@Value("${gateway.serviceName}")
	public String gatewayName;

	@Value("${uaos.serviceName}")
	public String uaosName;


	/*
		http://127.0.0.1:8082/test
	 */
	@GetMapping("/new")
	@HystrixCommand(fallbackMethod = "selectByIdFallback",
		commandProperties = {@HystrixProperty(name = "execution.isolation.thread.interruptOnTimeout", value = "true"),
				             @HystrixProperty(name = "fallback.enabled", value = "true")})
	public String selectById() {

		return RestUtil.execute(() -> {
			String url = RestUtil.genUrl(gatewayName, uaosName, "/uaos/api/v1/user?erp=chenhengyu1");
			System.out.println("###url: " + url);
            ResponseEntity<RestResponse<String>> responseEntity = restWrapper.get(url, new ParameterizedTypeReference<RestResponse<String>>() {});
            return responseEntity.getBody();
        }).getData().toString();

	}



	public String selectByIdFallback() {
		return "fall back";
	}

}
